const Koa = require('koa')
const path = require('path')
const nunjucks = require('koa-nunjucks-2')
const session = require('koa-session')
const bodyParser = require('koa-bodyparser')
const router = require('./routers/index')
const {config} = require('./redis/index')
const {valiateCookie} = require('./utils/upload')
const app = new Koa()

// session 设置
app.keys = ['leeCan']


app.use(session(config, app))
app.use(async (ctx, next) => {
  // console.log('ctx.path', ctx.path)
  ctx.session.agent = ctx.header['user-agent']
  return next()
})

app.use(bodyParser())
app.use(nunjucks({
  ext: 'html',
  path: path.join(__dirname, '/static/html'), // 指定视图目录
  nunjucksConfig: {
    trimBlocks: true, // 开启转义 防Xss
    autoescape: false
  }
}));
// 验证登陆状态中间件
app.use(valiateCookie)

// 抽离Router
app.use(router.routes())
  .use(router.allowedMethods())

// app.use(serve(__dirname + './static/html', {extensions: ['html']}))



app.listen(3000)